/**
 * 
 */
package com.demo.web.admin.interceptor;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.bluering.framework.common.log.Logger;
import com.demo.web.admin.form.FormResp;
import com.demo.web.admin.service.AuthService;

/**
 * 
 * 认证,授权拦截器
 * 
 * @author Administrator
 *
 */
public class AuthInterceptor implements HandlerInterceptor {
	
	private static Logger logger =Logger.getLogger(AuthInterceptor.class);
	
	@Autowired
	private AuthService authService;

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object)
	 */
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		
		String contentPath = request.getContextPath();
		String uri = request.getServletPath();
		
		logger.info("access url : {}",request.getRequestURL());
		logger.info("access content : {}",contentPath);
		logger.info("access ServletPath : {}",uri);
		
		
		return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, org.springframework.web.servlet.ModelAndView)
	 */
	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		if(modelAndView !=null) {
			modelAndView.addObject("contentPath", request.getContextPath());
		}
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.lang.Object, java.lang.Exception)
	 */
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}
	
	private boolean isAjaxRequest(HttpServletRequest req) {
		boolean isAjaxRequest = false;
		if(!StringUtils.isBlank(req.getHeader("x-requested-with")) && req.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")){
            isAjaxRequest = true;
        }
		return isAjaxRequest;
	}
	
	private void sendMessage(HttpServletRequest request,HttpServletResponse response,FormResp form) {
		if(isAjaxRequest(request)) {
			response.setCharacterEncoding("UTF-8");  
		    response.setContentType("application/json; charset=utf-8");
		    PrintWriter out = null;  
		    try {
		        out = response.getWriter();
		        out.append(JSON.toJSONString(form));
		    } catch (IOException e) {
		       logger.error(e.getMessage(),e);
		    } finally {
		        if (out != null) {
		            out.close();
		        }
		    }
		}else {
			try {
				response.sendRedirect(form.getRedirectUri());
			} catch (IOException e) {
				logger.error(e.getMessage(),e);
			}
		}
	}

}
